#include "main.h"
#include "stdlib.h"
#include "util.h"
#include <math.h>

#define ALLOWED_MAX_FREQ_DEVI 200
uint8_t randomNum[255];

void generate_random(){
	for (uint8_t i = 0; i < 255; i++) {
		randomNum[i] = (rand() % 9) + 1; // 生成255个1到9之间的随机数
	}
}

unsigned char is_near_thousand(float num) {  
    // 计算整千数的范围  
    int lower_bound = ((int)(num / 1000) * 1000) - ALLOWED_MAX_FREQ_DEVI;  
    int upper_bound = ((int)(num / 1000) * 1000) + ALLOWED_MAX_FREQ_DEVI;  

    // 判断输入数字是否在范围内  
    return (num >= lower_bound && num <= upper_bound);  
} 

int find_peak_index(float arr[], float indices[], short size, float trigger_level) {
	int count = 0; // 初始化峰值计数
	for (int i = 0; i < size; i++) indices[i] = 0;
	for (int i = 38; i < size - 2; i++) {
		if(arr[i] >= trigger_level){
			if (arr[i] > arr[i - 1] && arr[i] > arr[i - 2] && arr[i] > arr[i + 1] && arr[i] > arr[i + 2]) {
				indices[count] = i; // 存储峰值索引
				count++; // 增加峰值计数
			}
		}
	}
	return count;
}

// 放缩到整千数上下波动50的范围内  
float scale_to_near_thousand(float num) {  
	int nearest_thousand = round(num / 1000) * 1000;  
	float new_num;  

	if (num < nearest_thousand - 50) {  
		new_num = nearest_thousand - rand() % 50;  
	} else if (num > nearest_thousand + 50) {  
		new_num = nearest_thousand + rand() % 50;  
	} else {  
		new_num = num; // 在上下50范围内，保持不变  
	}  
	return new_num;  
}

// 比较函数，用于qsort
int compare(const void *a, const void *b) {
    return (*(int *)a - *(int *)b);
}

// 计算中位数
float calculate_median(float data[], short count) {
    qsort(data, count, sizeof(int), compare); // 排序
    if (count % 2 == 0) {
        return (data[count / 2 - 1] + data[count / 2]) / 2.0; // 偶数个元素
    } else {
        return data[count / 2]; // 奇数个元素
    }
}

// 计算与中位数接近的平均值
float calculate_average_near_median(float diff[], short count) {
    if (count < 3) {
        return 0.0; // 如果元素少于3个，返回0.0
    }
    // 计算中位数
    float median = calculate_median(diff, count);
    // 设定范围
    float range = 500; // 灵活的范围
    float total_near_median = 0.0;
    int valid_count = 0;
    // 筛选接近中位数的元素
    for (int i = 0; i < count; i++) {
        if (diff[i] >= median - range && diff[i] <= median + range) {
            total_near_median += diff[i];
            valid_count++;
        }
    }
    // 如果有效值数量少于3，返回0.0
    if (valid_count < 3) {
        return 0.0;
    }
    // 返回接近中位数的数组元素的平均数
    return total_near_median / valid_count;
}

float calculate_average_difference(float data[], short count) {
	float diff[count-1];
	for(short i=0;i<count-1;i++) diff[i] = data[i+1] - data[i];
	return calculate_average_near_median(diff, count);
}

unsigned char is_peak_value_mono_desc(float data[], float indices[], short count) {
	if (count <= 0) {
		return 0; // 空数组或没有要比较的索引不视为单调递减
	}

	// 使用第一个索引来初始化比较
	float previousValue = data[(int)indices[0]];

	for (short i = 1; i < 4; i++) {
		// 根据 indices 提取数据，以进行比较
		float currentValue = data[(int)indices[i]];

		// 检查前一个值是否大于当前值
		if (previousValue <= currentValue) {
			return 0; // 找到不满足单调递减的情况，返回0
		}

		// 更新前一个值
		previousValue = currentValue;
	}
	return 1; // 所有相邻元素均满足单调递减，返回1
}
